/**
 *
 */
package uk.co.hs.service.admin.interfaces;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import uk.co.hs.dao.web.admin.WebForwardingDao;
import uk.co.hs.domain.web.WebForwarding;
import uk.co.hs.web.interceptor.TabsAccessibilityInterceptor;

/**
 */
@Service("hs_webForwardingService")
@Transactional(value="hs_transactionManager")
public class WebForwardingServiceImpl implements WebForwardingServiceInterface
{
  @Resource
  private WebForwardingDao mWebForwardingDao;

  @Resource
  private WebUserServiceInterface mWebUserServiceInterface;;

  @Resource
  private WebUrlServiceInterface mWebUrlServiceInterface;

  /**
   * Gets the Web Forwarding object for a given Username.
   */
  public WebForwarding getWebForwarding(String aUsername)
  {
    WebForwarding wf = mWebForwardingDao.getWebForwarding(aUsername);

    //If the user doesn't have a saved forwarding preference, forward to the dashboard by default.
    if (wf == null)
    {
      wf = new WebForwarding();
      wf.setWebUser(mWebUserServiceInterface.getWebUser(aUsername));
      wf.setWebUrl(mWebUrlServiceInterface.getByUrl(TabsAccessibilityInterceptor.DASHBOARD_URL));
    }

    return wf;
  }

  /**
   * @see uk.co.hs.service.admin.interfaces.WebForwardingServiceInterface
   * #saveOrUpdate(uk.co.hs.domain.web.WebForwarding)
   * {@inheritDoc}
   */
  public void saveOrUpdate(WebForwarding aWebForwarding)
  {
    mWebForwardingDao.makePersistent(aWebForwarding);
  }
}
